/*  
* Copyright (c) 2010, James Daniello
* This project is licensed under the 'New BSD' license.
* The full text of the license can be found in the root
* directory under the filename "LICENSE"
*/
package src.gui;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;
import java.awt.Container;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

public class PICAIUClientGUIAddServer implements PICAIUGUI{

  //The various gui components.
  private Container frame;       //<-- the main panel container
  private JPanel content;
  private PICAIUClientGUI parent;
  
  private JTextField host;
  private JFormattedTextField port;
  
  
  /**
  * Creates the gui component for when adding a server
  */
  public PICAIUClientGUIAddServer(Container frameIn, PICAIUClientGUI parentIn){
    frame = frameIn;
    parent = parentIn;
    initGUI();
  }
  
  
  /**
  * creates the gui components
  */
  public void initGUI(){
  
    content = new JPanel();
    
    JPanel padding1 = new JPanel();
    padding1.setSize(750,55);
    padding1.setPreferredSize(new java.awt.Dimension(750,55));
    padding1.setMaximumSize(new java.awt.Dimension(750,55));
    padding1.setMinimumSize(new java.awt.Dimension(750,55));
    content.add(padding1);
    
    
    JLabel logo = new JLabel("                               Add a Server                               ");
    Font f = new Font("Dialog", Font.PLAIN, 32);
    logo.setFont(f);
    content.add(logo);
    
    JPanel padding2 = new JPanel();
    padding2.setSize(750,45);
    padding2.setPreferredSize(new java.awt.Dimension(750,45));
    padding2.setMaximumSize(new java.awt.Dimension(750,45));
    padding2.setMinimumSize(new java.awt.Dimension(750,45));
    content.add(padding2);
    
    JPanel addPanel = new JPanel(new GridBagLayout());
    GridBagConstraints con = new GridBagConstraints();
    con.gridx=0;
    con.gridy=0;
  
    host = new JTextField();
    host.setColumns(25);
    addPanel.add(new JLabel("                                                 Host: "),con);
    con.gridx=1;
    addPanel.add(host,con);
    con.gridy=1;
    con.gridx=0;
    addPanel.add(new JLabel("                                                 Port: "),con);
    port = new JFormattedTextField(NumberFormat.getIntegerInstance());
    port.setColumns(25);
    con.gridy=1;
    con.gridx=1;
    addPanel.add(port,con);
    content.add(addPanel);
    content.add(new JLabel("                                                           "));
    content.add(new JLabel("                                              "));
    JButton add = new JButton("Add");
    add.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent evt){
        if(!host.getText().equals("")&&!port.getText().equals("")){
          String hostText = host.getText().replace(" ","");
          String portText = port.getText().replace(",","");
          int portInt = Integer.parseInt(portText);
          parent.addServer(hostText,portInt);
          parent.setGUI("login recalc servers");
        }
      }
    });
    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent evt){
        parent.setGUI("login old");
      }
    });
    content.add(add);
    content.add(cancel);
  }
  
  /**
  * This method switches the clients main container to this GUI's graphics.
  */
  @Override
  public void setAsGUI(){
    host.setText("");
    port.setText("");
    frame.removeAll();
    frame.add(content,BorderLayout.CENTER);
    host.requestFocusInWindow();
    frame.validate();
  
  }

  /**
  * This sets the colors of all the components affected by the color scheme to that of the current 
  * colors for the GUI
  */
  @Override
  public void setColors(PICAIUGUIScheme colorScheme){
    
    Color colorBG01=colorScheme.getBG01();
    Color colorBG02=colorScheme.getBG02();
    Color colorFG01=colorScheme.getFG01();
    Color colorBorder01=colorScheme.getBorder01();
  
  }

}